package com.service.jkt.impl;

import com.dao.*;
import com.entity.*;
import com.service.jkt.ChaXunService;
import com.util.gzp.AjaxResponse;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@org.springframework.stereotype.Service
public class ChaXunServiceImpl implements ChaXunService {
    @Resource
    private ServiceDao serviceDao;
    @Resource
    private ServicetypeDao servicetypeDao;
    @Resource
    private ServicestatusDao servicestatusDao;
    @Resource
    private ClientDao clientDao;
    @Resource
    private DissentingregistrationDao dissentingregistrationDao;
    @Resource
    private OwnershipDao ownershipDao;
    @Resource
    private ExistmortgageDao existmortgageDao;
    @Resource
    private BeinmortgageDao beinmortgageDao;
    @Resource
    private CloseregistrationDao closeregistrationDao;
    @Resource
    private RoomDao roomDao;
    @Resource
    private RoomstatusDao roomstatusDao;
    @Resource
    private CloseofficeDao closeofficeDao;
    @Resource
    private CheckerDao checkerDao;
    @Resource
    private  OtherregistrationDao otherregistrationDao;
    @Resource
    private BuildingDao buildingDao;
    @Override
    public AjaxResponse findsyq(Integer serviceid) {
        //业务表
        Service service = findService(serviceid);
        //业务类型
        Servicetype st = findServicetype(service.getServicetypeid());

        String str = service.getServicenumber();
        //所有权表
        Ownership ownership = ownershipDao.findByServiceNumber(str);
//        //房屋表
//        Room room = roomDao.selectByPrimaryKey(ownership.getRoomid());
//        //楼栋表
//        Building building = buildingDao.selectByPrimaryKey(room.getBuildingid());
        //申请人
        Client proposer = clientDao.selectByPrimaryKey(service.getProposer());
        Map<String,Object> map =new HashMap<>();
        //房屋表
        if(ownership.getRoomid()!=null){
            Room room =roomDao.selectByPrimaryKey(ownership.getRoomid());
            map.put("room",room);
            //楼栋表
            Building building = buildingDao.selectByPrimaryKey(room.getBuildingid());
            map.put("building",building);
            //房屋状态表
            Roomstatus roomstatus = roomstatusDao.selectByPrimaryKey(room.getRoomstatusid());
            map.put("roomstatus",roomstatus);
        }
        //代理人名（如果有就查）
        if(service.getAgent()!=null) {
            Client agent = clientDao.selectByPrimaryKey(service.getAgent());
            map.put("agent",agent);
        }
        map.put("proposer",proposer);

        //如果已经审核或审批，就查询审核人表
        if(service.getServicestatusid()==3||service.getServicestatusid()==6) {
            Checker checker = checkerDao.findByServiceid(serviceid);
            map.put("checker",checker);
            //审核人名
            Client cli1 = clientDao.selectByPrimaryKey(checker.getReviewerid());
            String reviewer = cli1.getClientname();
            map.put("reviewer",reviewer);
            if(service.getServicestatusid()==3) {
                //终审人名
                Client cli2 = clientDao.selectByPrimaryKey(Integer.valueOf(checker.getFinalreviewerid()));
                String finalreviewer = cli2.getClientname();
                map.put("finalreviewer",finalreviewer);
            }
        }
        map.put("service",service);
        map.put("servicetype",st);
        map.put("ownership",ownership);

        return AjaxResponse.success(map,"success");
    }

    @Override
    public AjaxResponse findxf(Integer serviceid) {
        //业务表
        Service service = findService(serviceid);
        //业务类型
        Servicetype st = findServicetype(service.getServicetypeid());
        //他权项现房
        Existmortgage ex = existmortgageDao.findByServiceid(serviceid);
        //抵押权人名
        Client c1 = clientDao.selectByPrimaryKey(ex.getMortgagee());
        String mortgagee = c1.getClientname();
        //抵押人名
        Client c2 = clientDao.selectByPrimaryKey(ex.getMortgager());
        String mortgager = c2.getClientname();
        //债务人
        Client c3 = clientDao.selectByPrimaryKey(ex.getDebtor());
        String debtor = c3.getClientname();

//        //房屋表
//        Room room = roomDao.selectByPrimaryKey(ex.getRoomid());
        //房屋状态表
 //       Roomstatus roomstatus = roomstatusDao.selectByPrimaryKey(room.getRoomstatusid());
//        //楼栋表
//        Building building = buildingDao.selectByPrimaryKey(room.getBuildingid());
        //申请人名
        Client proposer = clientDao.selectByPrimaryKey(service.getProposer());
        Map<String,Object> map =new HashMap<>();
        //房屋表
        if(ex.getRoomid()!=null){
            Room room =roomDao.selectByPrimaryKey(ex.getRoomid());
            map.put("room",room);
            //楼栋表
            Building building = buildingDao.selectByPrimaryKey(room.getBuildingid());
            map.put("building",building);
            //房屋状态表
            Roomstatus roomstatus = roomstatusDao.selectByPrimaryKey(room.getRoomstatusid());
            map.put("roomstatus",roomstatus);
        }
        //代理人名（如果有就查）
        if(service.getAgent()!=null) {
            Client agent = clientDao.selectByPrimaryKey(service.getAgent());
            map.put("agent",agent);
        }
        map.put("proposer",proposer);

        //如果已经审核或审批，就查询审核人表
           if(service.getServicestatusid()==3||service.getServicestatusid()==6) {
            Checker checker = checkerDao.findByServiceid(serviceid);
            map.put("checker",checker);
            //审核人名
            Client cli1 = clientDao.selectByPrimaryKey(checker.getReviewerid());
            String reviewer = cli1.getClientname();
            map.put("reviewer",reviewer);
            if(service.getServicestatusid()==3) {
                //终审人名
                Client cli2 = clientDao.selectByPrimaryKey(Integer.valueOf(checker.getFinalreviewerid()));
                String finalreviewer = cli2.getClientname();
                map.put("finalreviewer",finalreviewer);
            }
        }
        map.put("service",service);
        map.put("servicetype",st);
        map.put("existmortgage",ex);
        map.put("mortgagee",mortgagee);
        map.put("mortgager",mortgager);
        map.put("debtor",debtor);
        return AjaxResponse.success(map,"success");
    }

    @Override
    public AjaxResponse findzj(Integer serviceid) {
        //业务表
        Service service = findService(serviceid);
        //业务类型
        Servicetype st = findServicetype(service.getServicetypeid());
        //他权项在建表
        Beinmortgage be = beinmortgageDao.findByServiceid(serviceid);
        //抵押权人名
        Client c1 = clientDao.selectByPrimaryKey(be.getMortgagee());
        String mortgagee = c1.getClientname();
        //抵押人名
        Client c2 = clientDao.selectByPrimaryKey(be.getMortgager());
        String mortgager = c2.getClientname();

//        //房屋表
//        Room room = roomDao.selectByPrimaryKey(be.getRoomid());
//        //房屋状态表
//        Roomstatus roomstatus = roomstatusDao.selectByPrimaryKey(room.getRoomstatusid());
//        //楼栋表
//        Building building = buildingDao.selectByPrimaryKey(room.getBuildingid());

        //申请人名
        Client proposer = clientDao.selectByPrimaryKey(service.getProposer());
        Map<String,Object> map =new HashMap<>();
        //房屋表
        if(be.getRoomid()!=null){
            Room room =roomDao.selectByPrimaryKey(be.getRoomid());
            map.put("room",room);
            //楼栋表
            Building building = buildingDao.selectByPrimaryKey(room.getBuildingid());
            map.put("building",building);
            //房屋状态表
            Roomstatus roomstatus = roomstatusDao.selectByPrimaryKey(room.getRoomstatusid());
            map.put("roomstatus",roomstatus);
        }
        //代理人名（如果有就查）
        if(service.getAgent()!=null) {
            Client agent = clientDao.selectByPrimaryKey(service.getAgent());
            map.put("agent",agent);
        }
        map.put("proposer",proposer);
        //如果已经审核或审批，就查询审核人表
        if(service.getServicestatusid()==3||service.getServicestatusid()==6) {
            Checker checker = checkerDao.findByServiceid(serviceid);
            map.put("checker",checker);
            //审核人名
            Client cli1 = clientDao.selectByPrimaryKey(checker.getReviewerid());
            String reviewer = cli1.getClientname();
            map.put("reviewer",reviewer);
            if(service.getServicestatusid()==3) {
                //终审人名
                Client cli2 = clientDao.selectByPrimaryKey(Integer.valueOf(checker.getFinalreviewerid()));
                String finalreviewer = cli2.getClientname();
                map.put("finalreviewer",finalreviewer);
            }
        }
        map.put("service",service);
        map.put("servicetype",st);
        map.put("beinmortgage",be);
        map.put("mortgagee",mortgagee);
        map.put("mortgager",mortgager);
        return AjaxResponse.success(map,"success");
    }

    @Override
    public AjaxResponse findcf(Integer serviceid) {
        //业务表
        Service service = findService(serviceid);
        //业务类型
        Servicetype st = findServicetype(service.getServicetypeid());
        //查封表
        Closeregistration cl = closeregistrationDao.findByServiceid(serviceid);
        //所有查封机关
         List<Closeoffice> cce = closeofficeDao.findAll();

        //申请人名
        Client proposer = clientDao.selectByPrimaryKey(service.getProposer());
        Map<String,Object> map =new HashMap<>();
        //当前查封机关种类表
        if(cl.getCloseofficeid()!=null) {
            Closeoffice ce = closeofficeDao.selectByPrimaryKey(cl.getCloseofficeid());
            map.put("thiscloseoffice",ce);
        }
        //房屋表
        if(cl.getDescript()!=null) {
            Room room = roomDao.findByRoomnumber(cl.getDescript());
            map.put("room",room);   //房屋状态表
            Roomstatus roomstatus = roomstatusDao.selectByPrimaryKey(room.getRoomstatusid());
            //楼栋表
            Building building = buildingDao.selectByPrimaryKey(room.getBuildingid());
            map.put("roomstatus",roomstatus);
            map.put("building",building);
        }
        //代理人名（如果有就查）
        if(service.getAgent()!=null) {
            Client agent = clientDao.selectByPrimaryKey(service.getAgent());
            map.put("agent",agent);
        }
        map.put("proposer",proposer);
        //如果已经审核或审批，就查询审核人表
        if(service.getServicestatusid()==3||service.getServicestatusid()==6) {
            Checker checker = checkerDao.findByServiceid(serviceid);
            map.put("checker",checker);
            //审核人名
            Client cli1 = clientDao.selectByPrimaryKey(checker.getReviewerid());
            String reviewer = cli1.getClientname();
            map.put("reviewer",reviewer);
            if(service.getServicestatusid()==3) {
                //终审人名
                Client cli2 = clientDao.selectByPrimaryKey(Integer.valueOf(checker.getFinalreviewerid()));
                String finalreviewer = cli2.getClientname();
                map.put("finalreviewer",finalreviewer);
            }
        }
        map.put("service",service);
        map.put("servicetype",st);
        map.put("closeregistration",cl);
        map.put("closeoffice",cce);


        return AjaxResponse.success(map,"success");
    }

    @Override
    public AjaxResponse findyy(Integer serviceid) {
        //业务表
        Service service = findService(serviceid);
        //业务类型
        Servicetype st = findServicetype(service.getServicetypeid());
        //异议
        Dissentingregistration di = dissentingregistrationDao.findByServiceid(serviceid);

        //申请人名
        Client proposer = clientDao.selectByPrimaryKey(service.getProposer());
        Map<String,Object> map =new HashMap<>();
        //房屋表
        if(di.getDescript()!=null) {
            Room room = roomDao.findByRoomnumber(di.getDescript());
            map.put("room",room);   //房屋状态表
            Roomstatus roomstatus = roomstatusDao.selectByPrimaryKey(room.getRoomstatusid());
            //楼栋表
            Building building = buildingDao.selectByPrimaryKey(room.getBuildingid());
            map.put("roomstatus",roomstatus);
            map.put("building",building);
        }
        //代理人名（如果有就查）
        if(service.getAgent()!=null) {
            Client agent = clientDao.selectByPrimaryKey(service.getAgent());
            map.put("agent",agent);
        }
        map.put("proposer",proposer);
        //如果已经审核或审批，就查询审核人表
        if(service.getServicestatusid()==3||service.getServicestatusid()==6) {
            Checker checker = checkerDao.findByServiceid(serviceid);
            map.put("checker",checker);
            //审核人名
            Client cli1 = clientDao.selectByPrimaryKey(checker.getReviewerid());
            String reviewer = cli1.getClientname();
            map.put("reviewer",reviewer);
            if(service.getServicestatusid()==3) {
                //终审人名
                Client cli2 = clientDao.selectByPrimaryKey(Integer.valueOf(checker.getFinalreviewerid()));
                String finalreviewer = cli2.getClientname();
                map.put("finalreviewer",finalreviewer);
            }
        }
        map.put("service",service);
        map.put("servicetype",st);
        map.put("dissentingregistration",di);
        return AjaxResponse.success(map,"success");
    }

    @Override
    public AjaxResponse findjf(Integer serviceid) {
        return findjf(serviceid);
    }

    @Override
    public AjaxResponse findyg(Integer serviceid) {
        //业务表
        Service service = findService(serviceid);
        //业务类型
        Servicetype st = findServicetype(service.getServicetypeid());
        //预告表
        Otherregistration oth = otherregistrationDao.findByServiceid(serviceid);

        //申请人名
        Client proposer = clientDao.selectByPrimaryKey(service.getProposer());
        Map<String,Object> map =new HashMap<>();
        //房屋表
        if(service.getPhoto()!=null&&service.getPhoto()!=""){
            Room room =roomDao.selectByPrimaryKey(Integer.valueOf(service.getPhoto()));
            map.put("room",room);
            //楼栋表
            Building building = buildingDao.selectByPrimaryKey(room.getBuildingid());
            map.put("building",building);
            //房屋状态表
            Roomstatus roomstatus = roomstatusDao.selectByPrimaryKey(room.getRoomstatusid());
            map.put("roomstatus",roomstatus);
        }

        if(oth.getObligee()!=null&&oth.getObligor()!=null){
            //权利人
            Client obligee = clientDao.selectByPrimaryKey(oth.getObligee());
            //义务人
            Client obligor = clientDao.selectByPrimaryKey(oth.getObligor());
            map.put("obligee",obligee);
            map.put("obligor",obligor);
        }

        //代理人名（如果有就查）
        if(service.getAgent()!=null) {
            Client agent = clientDao.selectByPrimaryKey(service.getAgent());
            map.put("agent",agent);
        }
        map.put("proposer",proposer);
        //如果已经审核或审批，就查询审核人表
        if(service.getServicestatusid()==3||service.getServicestatusid()==6) {
            Checker checker = checkerDao.findByServiceid(serviceid);
            map.put("checker",checker);
            //审核人名
            Client cli1 = clientDao.selectByPrimaryKey(checker.getReviewerid());
            String reviewer = cli1.getClientname();
            map.put("reviewer",reviewer);
            if(service.getServicestatusid()==3) {
                //终审人名
                Client cli2 = clientDao.selectByPrimaryKey(Integer.valueOf(checker.getFinalreviewerid()));
                String finalreviewer = cli2.getClientname();
                map.put("finalreviewer",finalreviewer);
            }
        }
        map.put("service",service);
        map.put("servicetype",st);
        map.put("otherregistration",oth);
        return AjaxResponse.success(map,"success");
    }

    //公用方法
    public Service findService(Integer serviceid){
        return serviceDao.selectByPrimaryKey(serviceid);
    }
    public Servicetype findServicetype(Integer servicetypeid){
        return servicetypeDao.selectByPrimaryKey(servicetypeid);
    }
}
